#include "actions.h"

extern loginUi *login;

int actions::checkStats(QStringList &frame) {
	QString Par;
	Par = frame.front();
	frame.pop_front();
	
	if(login->win.id == Par.toInt()) {
		return 1;
	} else {
		printf("RESPOSTA VEIO PARA UM CLIENTE ERRADO\n");
		return 0;	
	}
}

void actions::registerWellAction(QStringList frame) {
	QString Par;
	checkStats(frame);
	
	Par = frame.front();
	frame.pop_front();
	if(Par.toInt()) {
		QMessageBox::information(login,"Cadastro de Poco","Poco Cadastrado com Suscesso");
		login->win.requestWellList();
		login->ui.hide();
	} else {
		QMessageBox::information(login,"Cadastro de Poco","Poco Nao pode ser Cadastrado");
	}
}

void actions::loginAux(int nivel) {
	printf("NO HAND SHAKE PORRA %i\n",nivel);
	switch (nivel) {
		case 2:
			login->win.actionCadastrar_Poco->setEnabled(false);
			login->win.actionDelete_Well->setEnabled(false);
		break;
		case 1:
			login->win.actionCadastrar_Poco->setEnabled(false);
			login->win.actionDelete_Well->setEnabled(false);		
		break;
		default:
		break;
	}
	login->win.userLevel = nivel;
}

void actions::loginAction(QStringList frame) {
	QString Par;	
	QStringList Param;
	
	Par = frame.front();
	frame.pop_front();
	login->win.id = Par.toInt();
	
	
	Par = frame.front();
	frame.pop_front();
	printf("RECENI UM HAND SHAKE\n");
	if(Par.toInt() == 0) {
		login->lb_status->setText("LOGIN OU SENHA INVALIDOS");
	} else {
		login->win.show();
		login->hide();
		login->win.sendFunction(GET_MASTERS_LIST,Param);
		login->win.requestWellList();
		loginAux(Par.toInt());
	}
}

void actions::weelListAction(QStringList frame) {
	QString Par;
	int intPar;
	int numberOfParans;
	QTableWidgetItem *tmpItem;
		
	checkStats(frame);
	Par = frame.front();
	numberOfParans = Par.toInt();
	frame.pop_front(); //numberOfParans
	login->win.wellTable->setRowCount(numberOfParans);
	login->win.wellTable->setSortingEnabled(false);
	login->win.wMap.clear();
	for(int i = 0; i < numberOfParans; i++) {
		wells tmp;
		Par = frame.front();
		frame.pop_front(); //ID
		if(!login->win.wellTable->item(i,1)) {
		tmpItem = new QTableWidgetItem(Par);
		tmpItem->setFlags(Qt::ItemIsEnabled);
		tmpItem->setTextAlignment(Qt::AlignCenter);
		login->win.wellTable->setItem(i,1,tmpItem);;
		}
		else
			login->win.wellTable->item(i,1)->setText(Par);
		tmp.id = Par.toInt();
		Par = frame.front();
		intPar = Par.toInt();
		frame.pop_front(); //NOME 
		if(!login->win.wellTable->item(i,0)) {
			tmpItem = new QTableWidgetItem(Par);
			tmpItem->setFlags(Qt::ItemIsEnabled);
			tmpItem->setTextAlignment(Qt::AlignCenter);
			login->win.wellTable->setItem(i,0,tmpItem);;
		}
		else
			login->win.wellTable->item(i,0)->setText(Par);
		tmp.name = Par;
		tmp.state = -1;
		
		Par = frame.front();
		intPar = Par.toInt();
		frame.pop_front(); //Id Simulador 
		if(!login->win.wellTable->item(i,4)) {
			tmpItem = new QTableWidgetItem(login->win.sMap[intPar].name);
			tmpItem->setFlags(Qt::ItemIsEnabled);
			tmpItem->setTextAlignment(Qt::AlignCenter);
			login->win.wellTable->setItem(i,4,tmpItem);;
		}
		else
			login->win.wellTable->item(i,4)->setText(login->win.sMap[intPar].name);
		tmp.idSimulador = intPar;
		
		Par = frame.front();
		frame.pop_front(); //TimeOut 
		if(!login->win.wellTable->item(i,5)) {
			tmpItem = new QTableWidgetItem(Par);
			if(login->win.userLevel == 1)
				tmpItem->setFlags(Qt::ItemIsEnabled);
			tmpItem->setTextAlignment(Qt::AlignCenter);
			login->win.wellTable->setItem(i,5,tmpItem);;
		}
		else{
			login->win.wellTable->item(i,5)->setText(Par);
		}


		tmp.timeout = Par.toInt();
		tmp.state = -1;
		login->win.wMap[tmp.name] = tmp;
		
	}
	//login->win.wellTable->setSortingEnabled(true);
	login->win.wellTable->resizeColumnsToContents();	
	login->win.getStateWells();
}


void actions::getStateWellAction(QStringList frame) {
	QString wellId;
	int state;
	int i = 0;
	checkStats(frame);
	
	wellId = frame.front(); //wellId
	printf("RECEDENDO VALOR DO POCO %s\n",wellId.toStdString().c_str());
	frame.pop_front();
	state = frame.front().toInt(); //state 
	login->win.wMap[wellId].state = state;
	while(login->win.wellTable->item(i,0)->text() != login->win.wMap[wellId].name)
		i++;
	login->win.wellTable->setSortingEnabled(false);
	if(!login->win.wellTable->item(i,3)) {
		QTableWidgetItem *tmpItem = new QTableWidgetItem(login->win.wMap[wellId].getStringState());
		tmpItem->setFlags(Qt::ItemIsEnabled);
		tmpItem->setTextAlignment(Qt::AlignCenter);
		login->win.wellTable->setItem(i,3,tmpItem);;	
	} else {
		login->win.wellTable->item(i,3)->setText(login->win.wMap[wellId].getStringState());
	}
	//login->win.wellTable->setSortingEnabled(true);
	login->win.wellTable->resizeColumnsToContents();
	login->win.getAlarmsWells();
}


void actions::getMasterListAction(QStringList frame) {
	int numberOfMasters;	
	checkStats(frame);
	
	numberOfMasters = frame.front().toInt();
	frame.pop_front();
	
	for(int i = 0; i < numberOfMasters; i++) {
		simuladores tmp;
		tmp.id = frame.front().toInt();
		frame.pop_front();
		tmp.ip = frame.front();
		frame.pop_front();
		tmp.name = frame.front();		
		printf("SE LIGA ALAN %s\n",tmp.name.toStdString().c_str());
		frame.pop_front();
		login->win.sMap[tmp.id] = tmp;
	}
	printf("quantidade de simuladores %i\n",login->win.sMap.size());
}

void actions::getAllDataFromWellAction(QStringList frame) {
	QString idWell;	
	checkStats(frame);
	
	idWell = frame.front();
	printf("ALAN PORRA %s\n",frame.front().toStdString().c_str());
	frame.pop_front();
	
	for(int i = 0; i < 12; i++) {
		login->win.wMap[idWell].onlineVariable[i] = frame.front();
		printf("%s\n",frame.front().toStdString().c_str());
		frame.pop_front(); 
	}
	
	//GET_ALL_DATA_FROM_WELL
	login->sup.setup(login->win.wMap[idWell]);
	printf("No Data soo mto loco %s\n",idWell.toStdString().c_str());
}

void actions::getLetterFromWellAction(QStringList frame) {
	QString idWell;
	checkStats(frame);
	
	idWell = frame.front();
	frame.pop_front();
	
	login->win.wMap[idWell].l.seconds = frame.front().toInt();
	frame.pop_front();
	if(login->win.wMap[idWell].l.seconds == -1)
		return; //erro au pegar a carta

	login->win.wMap[idWell].l.numberOfPoints = frame.front().toInt();
	frame.pop_front();	
	
	login->win.wMap[idWell].l.maxScale = frame.front().toInt();
	frame.pop_front();
	
	login->win.wMap[idWell].l.minScale = frame.front().toInt();
	frame.pop_front();
	
	login->win.wMap[idWell].l.strokeLenght = frame.front().toInt();
	frame.pop_front();
	
	login->win.wMap[idWell].l.strokePeriod = frame.front().toInt();
	frame.pop_front();
	
	for(int i = 0; i < 200; i++) {
		login->win.wMap[idWell].l.x[i] = frame.front().toInt();
		frame.pop_front();
		login->win.wMap[idWell].l.y[i] = frame.front().toInt();
		frame.pop_front();
	}
	login->sup.drawChart(login->win.wMap[idWell]);
}

void actions::getDeleteWellAction(QStringList frame) {
	int status;	
	checkStats(frame);
	
	status = frame.front().toInt();
	if(status) {
		QMessageBox::information(login,"DeleteWell","Poco deletado com Suscesso");
		login->del.hide();
	} else {
		QMessageBox::information(login,"Delete Well","Poco Nao pode ser deletado");
	}
	
}

void actions::getAlarmsAction(QStringList frame) {
	QString wellId;
	int i = 0;
	checkStats(frame);
	
	wellId = frame.front(); frame.pop_front();//wellId
	printf("RECEDENDO VALOR DO POCO  nos alarmes %s\n",wellId.toStdString().c_str());
	for(int i = 0; i < 5; i++) {
		login->win.wMap[wellId].alarms[i] = frame.front().toInt(); frame.pop_front();//wellId
	}
	while(login->win.wellTable->item(i,0)->text() != login->win.wMap[wellId].name)
		i++;
		
	if(!login->win.wellTable->item(i,2)) {
		QTableWidgetItem *tmpItem = new QTableWidgetItem(login->win.wMap[wellId].getStringAlarms());
		tmpItem->setFlags(Qt::ItemIsEnabled);
		tmpItem->setTextAlignment(Qt::AlignCenter);
		login->win.wellTable->setItem(i,2,tmpItem);;	
	} else {
		login->win.wellTable->item(i,2)->setText(login->win.wMap[wellId].getStringAlarms());
	}
	login->win.wellTable->resizeColumnsToContents();
}



void actions::getHistoricAction(QStringList frame) {
	checkStats(frame);
	login->letter.setup(frame);
}


